package designpattern.singleton;

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
/**
 * 
 * @author BaoLoc
 *
 */
public class Singleton extends JFrame{
	private static int counter = 0;
	private static Singleton singleton =null;
	private JLabel label;
	
    //private hàm khởi tạo
	private Singleton(){
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setPreferredSize(new Dimension(200, 100));
		pack();
		setLocation(750, 280);
		label = new JLabel(new java.util.Date().toString());
		add(label);
		setVisible(true);
	}
	public static Singleton getInstance(){
		++counter ; // poll 
		if(singleton == null || counter < 5){
			singleton = new Singleton();
		}
		return singleton;
	}
	
	@Override
	public void dispose() {
		singleton =null; // Gán object lại null để xây dựng lại
		super.dispose();
	}
}
